package com.jeesite.modules.ab.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;

import com.jeesite.modules.ab.entity.WeatherInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 通过get请求向网站http://wthrcdn.etouch.cn/weather_mini获取某个 城市的天气状况数据，数据格式是Json
 *
 * @author yunjuanyunshuxue
 */
public class WeatherUtils {

    public static void main(String[] args) {
        WeatherInfo weatherInfo = WeatherUtils.GetWeather(WeatherUtils.GetWeatherData("101010100 "));
        System.out.println(weatherInfo.getTemperature());
    }
    /**
     * 通过城市名称获取该城市的天气信息
     *
     * @param cityname
     * @return
     */

    public static String GetWeatherData(String cityname) {
        StringBuilder sb = new StringBuilder();
        try {
            String weather_url = "http://wthrcdn.etouch.cn/weather_mini?citykey=" + cityname;
            System.out.println(weather_url);
            URL url = new URL(weather_url);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            GZIPInputStream gzin = new GZIPInputStream(is);
            InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式，自定义编码
            BufferedReader reader = new BufferedReader(isr);
            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line + " ");
            reader.close();
        } catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(sb.toString());
        return sb.toString();
    }


    /**
     * 将JSON格式数据进行解析 ，返回一个weather对象
     *
     * @param weatherInfobyJson
     * @return
     */
    public static WeatherInfo GetWeather(String weatherInfobyJson) {
        JSONObject dataOfJson = JSONObject.fromObject(weatherInfobyJson);
        if (dataOfJson.getInt("status") != 1000)
            return null;

//创建WeatherInfo对象，提取所需的天气信息
        WeatherInfo weatherInfo = new WeatherInfo();

//从json数据中提取数据
        String data = dataOfJson.getString("data");
        dataOfJson = JSONObject.fromObject(data);
        weatherInfo.setCityname(dataOfJson.getString("city"));

        weatherInfo.setTemperature(dataOfJson.getDouble("wendu"));
        return weatherInfo;
    }
}
